package com.project.finance.role.controller;

import com.project.finance.common.po.TbSysPermission;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultGenerator;
import com.project.finance.common.vo.ResultMsg;
import com.project.finance.role.service.PermissionService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class PermissionController {
    private static final Logger logger= LogManager.getLogger(PermissionController.class);
    @Autowired
    private PermissionService permissionService;
    //查询
//    @RequestMapping(value = "/permission",method = RequestMethod.GET)
//    public Result selectAll(){
//        List<TbSysPermission> permissions=permissionService.selectAll();
//        return ResultGenerator.apiSuccess(permissions);
//    }
    //添加
    @RequestMapping(value = "/permission",method=RequestMethod.POST)
    public Result insert(@RequestBody TbSysPermission tbSysPermission){
        int insert = permissionService.insert(tbSysPermission);
        return ResultGenerator.apiSuccess(insert);
    }
    //修改
    @RequestMapping(value = "/permission",method=RequestMethod.PUT)
    public Result update(@RequestBody TbSysPermission tbSysPermission){
        int i = permissionService.update(tbSysPermission);
        if (i>0){
            return ResultGenerator.apiSuccess("修改成功.....");
        }else {
            return ResultGenerator.apiSuccess("修改失败.....");
        }
    }
    //删除
    @RequestMapping(value = "/permission",method=RequestMethod.DELETE)
    public Result delete(@RequestParam("permissionId") int permissinId){
        int i = permissionService.delete(permissinId);
        if (i>0){
            return ResultGenerator.apiSuccess("删除成功****");
        }else{
            return ResultGenerator.apiSuccess("删除失败*****");
        }
    }
    //分页
    @RequestMapping(value = "/permission",method = RequestMethod.GET)
    public ResultMsg listPermissionByPageCondition(@RequestParam("pageSize")int pageSize,
                                                   @RequestParam("pageNum")int pageNum){
        ResultMsg resultMsg = permissionService.listPermissionByPage(pageSize, pageNum);
        resultMsg.setCode(0);
        return resultMsg;
    }
    //根据账号来查询用户信息
    @RequestMapping(value = "Permission",method = RequestMethod.GET)
    public Result getSysPermission(@RequestParam("name")String pername){
        logger.info("GET Name : " + pername);
        List<TbSysPermission> tbSysPermissions = permissionService.getSysPermission(pername);
        Result listResult = ResultGenerator.apiSuccess(tbSysPermissions);
        return listResult;
    }
}
